package com.sgb.packers.view.activity;

import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.luck.picture.lib.config.PictureConfig;
import com.sgb.link.router.PathTeam;
import com.sgb.packers.R;
import com.sgb.packers.databinding.PackersActivityPersonGetWorkBinding;
import com.sgb.packers.view.fragment.JobWorkEditFragment;
import com.sgb.packers.view.fragment.PointWorkEditFragment;
import com.sgb.packers.viewmodel.PersonGetWorkViewModel;
import com.swgk.core.common.base.TeamBaseActivity;
import com.swgk.core.view.adapter.CommPagerAdapter;

import java.util.ArrayList;

/**
 * @author younghow
 * @date 2021/11/12
 * description:个人记工添加页
 */
@Route(path = PathTeam.PersonGetWorkActivity)
public class PersonGetWorkActivity extends TeamBaseActivity<PersonGetWorkViewModel, PackersActivityPersonGetWorkBinding> {

    @Autowired(name = "projectName", desc = "项目名称")
    public String projectName;

    @Autowired(name = "year", desc = "年")
    public int year;

    @Autowired(name = "month", desc = "月")
    public int month;

    @Autowired(name = "day", desc = "日")
    public int day;

    @Autowired(name = "projectNo", desc = "项目的no")
    String projectNo;

    public MutableLiveData<Intent> mIntentLiveData = new MutableLiveData<>();
    public MutableLiveData<Intent> mPictureLiveData = new MutableLiveData<>();
    private int type;

    public void setType(int type) {
        this.type = type;
    }

    @Override
    protected Class<PersonGetWorkViewModel> createVM() {
        return PersonGetWorkViewModel.class;
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        mBinding.actionBar.setOnBackClickListener((view) -> {
            finish();
        });
        String[] title = {"点工", "包工"};
        mBinding.tlPackerChangeFirstPublishTitle.addTab(mBinding.tlPackerChangeFirstPublishTitle.newTab().setText(title[0]));
        mBinding.tlPackerChangeFirstPublishTitle.addTab(mBinding.tlPackerChangeFirstPublishTitle.newTab().setText(title[1]));

        ArrayList<Fragment> list = new ArrayList<>();
        list.add(PointWorkEditFragment.newInstance(projectName, year, month, day, projectNo));
        list.add(JobWorkEditFragment.newInstance(projectName, year, month, day, projectNo));
        CommPagerAdapter pagerAdapter = new CommPagerAdapter(getSupportFragmentManager(), list, title);
        mBinding.vpPersonGetWork.setAdapter(pagerAdapter);
        mBinding.tlPackerChangeFirstPublishTitle.setupWithViewPager(mBinding.vpPersonGetWork);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int layoutId() {
        return R.layout.packers_activity_person_get_work;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                if (type == 1){
                    mIntentLiveData.setValue(data);
                }else if (type == 2){
                    mPictureLiveData.setValue(data);
                }
            }
        }
    }
}